home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #1 / Ham Radio 2000.iso / ham2000 / hf / dsp / source / rdlod.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-06-15  |  3.4 KB  |  159 lines

  1. /*  RDLOD.C -- Reads Motorola DSP56000 linker load files
  2.  *
  3.  *  Copyright (C) by Alef Null 1990, 1991
  4.  *  Author(s): Jarkko Vuori, OH2LNS
  5.  *  Modification(s):
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include "dspio.h"
  12.  
  13. static FILE *fd;
  14.  
  15.  
  16. /* add extension to the filename */
  17. static char *AddExtension(char *FileName, const char *Extension) {
  18.     static char  Name[_MAX_PATH];
  19.     char    *s1;
  20.  
  21.     /* copy basename */
  22.     s1 = Name;
  23.     while(*FileName && *FileName != '.')
  24.         *s1++ = *FileName++;
  25.  
  26.     /* copy extension (if there are already no extension) */
  27.     strcpy(s1, !*FileName ? Extension : FileName);
  28.  
  29.     return(Name);
  30. }
  31.  
  32.  
  33. /*
  34.  * Opens linker file
  35.  */
  36. int OpenLodFile(char *name) {
  37.     if (!(fd = fopen(AddExtension(name, ".lod"), "rb"))) {
  38.     fprintf(stderr, "rdlod error: can't open file '%s'\n", AddExtension(name, ".lod"));
  39.     return (-1);
  40.     } else {
  41.     setvbuf(fd, NULL, _IOFBF, 8192);
  42.     return (0);
  43.     }
  44. }
  45.  
  46.  
  47. /* read next token from input */
  48. static char *ReadToken(void) {
  49.     static char line[80];
  50.     char c, *p;
  51.  
  52.     #define WHITESPACE(x) ((x) == ' ' || (x) == '\t' || (x) == '\r' || (x) == '\n')
  53.  
  54.     /* flush leading whitespace out */
  55.     do {
  56.     fread(&c, 1, 1, fd);
  57.     } while (WHITESPACE(c) && !feof(fd));
  58.  
  59.     /* then read next token */
  60.     p = line;
  61.     while (p < &line[80] && !feof(fd) && !WHITESPACE(c)) {
  62.     *p++ = c;
  63.     fread(&c, 1, 1, fd);
  64.     }
  65.     *p = '\0';
  66.  
  67.     return (line);
  68. }
  69.  
  70. /* convert ASCII-Hex argument to long number */
  71. static long HexToLong(char *data) {
  72.     long val;
  73.  
  74.     sscanf(data, "%lX", &val);
  75.  
  76.     return (val);
  77. }
  78.  
  79. /*
  80.  * Read next word
  81.  */
  82. int ReadWord(DATASPACE *space, unsigned *address, long *word) {
  83.     static DATASPACE currentspace;
  84.     static unsigned  currentaddress;
  85.     static enum {
  86.     hunt, spaceheader, collectdata, end
  87.     }             state;
  88.     char        *token;
  89.  
  90.     while (1)
  91.     switch (state) {
  92.     case hunt:
  93.         /* search preamble */
  94.         if (strcmp(ReadToken(), "_START")) {
  95.         fprintf(stderr, "rdlod error: expecting _START, not a valid Motorola load file\n");
  96.         return (-1);
  97.         } else {
  98.         ReadToken(); ReadToken(); ReadToken();    // flush out _START arguments
  99.  
  100.         if (strcmp(ReadToken(), "_DATA")) {
  101.             fprintf(stderr, "rdlod error: expecting _DATA, not a valid Motorola load file\n");
  102.             return (-1);
  103.         }
  104.         state = spaceheader;
  105.         }
  106.         break;
  107.  
  108.     case spaceheader:
  109.         /* data space header */
  110.         switch (*ReadToken()) {
  111.         case 'P': currentspace = p; break;
  112.         case 'X': currentspace = x; break;
  113.         case 'Y': currentspace = y; break;
  114.         default:
  115.         fprintf(stderr, "rdlod error: illegal data space, not a valid Motorola load file\n");
  116.         return (-1);
  117.         break;
  118.         }
  119.  
  120.         currentaddress = (unsigned)HexToLong(ReadToken());
  121.  
  122.         state = collectdata;
  123.         break;
  124.  
  125.     case collectdata:
  126.         /* read next data */
  127.         token = ReadToken();
  128.         if (!strcmp(token, "_DATA"))
  129.         state = spaceheader;
  130.         else if (!strcmp(token, "_END"))
  131.         state = end;
  132.         else if (!*token) {
  133.         fprintf(stderr, "rdlod error: inputfile too short, not a valid Motorola load file\n");
  134.         return (-1);
  135.         } else {
  136.         *space     = currentspace;
  137.         *address = currentaddress++;
  138.         *word     = HexToLong(token);
  139.         return (0);
  140.         }
  141.         break;
  142.  
  143.     case end:
  144.         /* end of inputfile */
  145.         return (1);
  146.         break;
  147.     }
  148. }
  149.  
  150.  
  151. /*
  152.  * Closes linker file
  153.  */
  154. int CloseLodFile(void) {
  155.     fclose(fd);
  156.  
  157.     return (0);
  158. }
  159.